package com.huawang.mdesign.model.service.util;

import org.apache.commons.lang3.StringUtils;
import org.jetbrains.annotations.Nullable;

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.chrono.ChronoZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Optional;

/**
 * @author admin
 */
public class DateUtil {

    public static final String DATE_TIME_PATTERN = "yyyy-MM-dd HH:mm:ss";

    private DateUtil() {
    }

    public static long str2Long(@Nullable String s) {
        return Optional.ofNullable(s)
                .filter(StringUtils::isNotBlank)
                .map(e -> DateTimeFormatter.ofPattern(DATE_TIME_PATTERN))
                .map(e -> LocalDateTime.parse(s, e))
                .map(e -> e.atZone(ZoneId.systemDefault()))
                .map(ChronoZonedDateTime::toInstant)
                .map(Instant::toEpochMilli)
                .orElse(-1L);
    }
}
